package com.wsseieiei.fireballgame.blocks;

import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

import javax.annotation.Nonnull;
import java.util.Random;

import static com.wsseieiei.fireballgame.init.BlockEntityInit.MY_MACHINE_BLOCK_ENTITY;

public class Backer {
    public static final class MyMachine extends BaseEntityBlock {
        public MyMachine(Properties props) {
            super(props);
        }
        @Nonnull
        @Override
        public RenderShape getRenderShape(@Nonnull BlockState state) {
            return RenderShape.MODEL;
        }
        @Override
        public BlockEntity newBlockEntity(@Nonnull BlockPos pos, @Nonnull BlockState state) {
            return new MyMachineEntity(pos, state);
        }
        @Override
        public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, @Nonnull BlockState state, @Nonnull BlockEntityType<T> blockEntityType) {
            return level.isClientSide() ? null : createTickerHelper(blockEntityType, MY_MACHINE_BLOCK_ENTITY.get(), MyMachineEntity::tick);
        }
    }
    public static final class MyMachineEntity extends BlockEntity {
        public MyMachineEntity(BlockEntityType<MyMachineEntity> type, BlockPos worldPosition, BlockState blockState) {
            super(type, worldPosition, blockState);
        }
        public MyMachineEntity(BlockPos worldPosition, BlockState blockState) {
            this(MY_MACHINE_BLOCK_ENTITY.get(), worldPosition, blockState);
        }
        private static int count = 0;
        public static void tick(Level level, BlockPos pos, BlockState state, MyMachineEntity entity) {
            count += 1;
            Player player=null;
            if (level != null) {
                player = level.getNearestPlayer(pos.getX(), pos.getY(), pos.getZ(), 2.0, false);
                if (player != null) {
                    final Random random = new Random();
                    level.getServer().getLevel(player.getLevel().dimension()).sendParticles(ParticleTypes.PORTAL, player.getX() + random.nextDouble(1), player.getY() + random.nextDouble(0.5d + count / 100d, 1.0d + count / 100d), player.getZ() + random.nextDouble(1), 7, 0, 0.0d, 0, 5 * count / 100d);
                }
                }
            if (count > 60) {
                count = 0;
//           if (level != null && !level.isClientSide()) {
                if (level != null) {
                    if (player != null) {
                        player.sendMessage(new TranslatableComponent("chat.fireballgame.backed"), Util.NIL_UUID);
                        player.teleportTo(level.getLevelData().getXSpawn(),level.getLevelData().getYSpawn(), level.getLevelData().getZSpawn());
                    }

                }
            }
        }
    }
}
